/**
 * Copyright (c) 2013-2019 Nikita Koksharov
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.redisson;

import java.util.concurrent.locks.Lock;

import org.redisson.api.RLock;
import org.redisson.api.RReadWriteLock;
import org.redisson.command.CommandAsyncExecutor;

/**
 * A {@code ReadWriteLock} maintains a pair of associated {@link
 * Lock locks}, one for read-only operations and one for writing.
 * The {@link #readLock read lock} may be held simultaneously by
 * multiple reader threads, so long as there are no writers.  The
 * {@link #writeLock write lock} is exclusive.
 *
 * Works in non-fair mode. Therefore order of read and write
 * locking is unspecified.
 *
 * redisson读写锁。读写锁本身不是锁结构，只是载有一个redisson读锁和一个redisson写锁。
 * !!!注意，redisson读锁和写锁不能直接获取，他们没有显式锁名称，必须通过读写锁来获取。!!!
 *
 * 注意特殊情况:一个线程持有写锁，然后重入读锁是允许的。
 *
 * redisson读写锁是非公平的。因此，如果当前读写锁是一个写锁，那么之后所有的读锁和写锁都会阻塞，且订阅同一个通道等待当前写锁释放。
 * 当前写锁一旦释放，是读锁先抢到还是写锁先抢到是无法确认的。
 *
 * @author Nikita Koksharov
 *
 */
public class RedissonReadWriteLock extends RedissonExpirable implements RReadWriteLock {

    /**
     * 读写锁构造器，读写锁可能显式指定锁名
     */
    public RedissonReadWriteLock(CommandAsyncExecutor commandExecutor, String name) {
        super(commandExecutor, name);
    }

    /**
     * 获取读写锁上的读锁。必须通过读写锁获得。锁名就是读写锁的锁名
     * @return
     */
    @Override
    public RLock readLock() {
        return new RedissonReadLock(commandExecutor, getName());
    }

    /**
     * 获取读写锁上的写锁。必须通过读写锁获得。锁名就是读写锁的锁名
     * @return
     */
    @Override
    public RLock writeLock() {
        return new RedissonWriteLock(commandExecutor, getName());
    }

}
